<?php
class CodeSpace_Oauth_Server_Gateway extends CodeSpace_Oauth_Server
{
    private $_consumerKey = null;
    private $_consumerSecret = null;
    private $_accessToken = null;
    private $_accessTokenSecret = null;
    
    public function __construct($options = null)
    {
        parent::__construct($options);
    }
    
    private function _prepareOauthParameters()
    {
        $params = array ();
        $params['oauth_consumer_key'] = $this->_consumerKey;
        $params['oauth_nonce'] = md5(uniqid(rand(), true));
        $params['oauth_signature_method'] = 'HMAC-SHA1';
        $params['oauth_timestamp'] = time();
        $params['oauth_token'] = $this->_accessToken;
        $params['oauth_version'] = '1.0';
         
        return $params;
    }
    
    public function signRequest(array $requestParameters)
    {
        $oauthParameters = $this->_prepareOauthParameters();
        $requestParameters = array_merge($oauthParameters, $requestParameters);
        
        $utility = new Zend_Oauth_Http_Utility;
        $sign = $utility->sign(
            $requestParameters,
            'HMAC-SHA1',
            $this->_consumerSecret,
            $this->_accessTokenSecret,
            'POST',
            'http://morpheus.gateway.mkb.com:41000/service'
        );
        
        $requestParameters['oauth_signature'] = $sign;
        
        return $requestParameters;
    }
    
    public function setRequest()
    {
        $requestUrl = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REDIRECT_URL'];
        
        switch ($requestUrl)
        {
            case $this->_requestTokenRequestUrl:
                $request = new CodeSpace_Oauth_Server_Request_Token_Request();
                $this->_requestType = self::REQUEST_TOKEN_REQUEST;
                break;
            case $this->_accessTokenRequestUrl:
                $request = new CodeSpace_Oauth_Server_Request_Token_Access();
                $this->_requestType = self::REQUEST_TOKEN_ACCESS;
                break;    
            case $this->_authorizatioRequestUrl:
                $request = new CodeSpace_Oauth_Server_Request_Authorization();
                $this->_requestType = self::REQUEST_AUTHORIZATION;
                break;
            case 'authorizationcallback':
                $this->_processAuthorizationCallback();
                break;
            default:
                $request = new CodeSpace_Oauth_Server_Request_Transmission();
                break;
        }
        
        $this->_request = $request;
    }
    
    public function setConsumerKey($key)
    {
        $this->_consumerKey = $key;
    }
    
    public function setConsumerSecret($secret)
    {
        $this->_consumerSecret = $secret;
    }
    
    public function setAccessToken($token)
    {
        $this->_accessToken = $token;
    }
    
    public function setAccessTokenSecret($secret)
    {
        $this->_accessTokenSecret = $secret;
    }
    
    public function getConsumerKey()
    {
        return $this->_consumerKey;
    }
    
    public function getConsumerSecret()
    {
        return $this->_consumerSecret;
    }
    
    public function getAccessToken()
    {
        return $this->_accessToken;
    }
    
    public function getAccessTokenSecret()
    {
        return $this->_accessTokenSecret;
    }
}
?>